#include "func_adc.h"
#include "string.h"
#include "usb_com.h"
#include "my_timer.h"
#include "adc.h"
#define ADC_MAX_NUM 4
struct my_timer adc_time;
unsigned short ADC_Values[ADC_MAX_NUM];
extern ADC_HandleTypeDef hadc1;

void adc_loop(void *p)
{
    usb_cmd(COMM_ADC,COMM_SUB_ADC_UPDATE,0);
}

void adc_init()
{
    timer_register_set(&adc_time, 1000, 0, 0, adc_loop, 0, 1);
    HAL_ADCEx_Calibration_Start(&hadc1);//ADC????
    HAL_Delay(200);//??200ms
    HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_Values, ADC_MAX_NUM);
}
void adc_send_data(unsigned char cmd,unsigned char subCmd,void*param)
{
    unsigned char sendData[14] = {0};
    sendData[0] = 0x55;
    sendData[1] = 0xaa;
    sendData[2] = 14;
    sendData[3] = COMM_ADC;
    sendData[4] = COMM_SUB_ADC_UPDATE;
    sendData[5] = ADC_Values[0] >> 8;
    sendData[6] = ADC_Values[0] & 0xff;
    sendData[7] = ADC_Values[1] >> 8;
    sendData[8] = ADC_Values[1] & 0xff;
    sendData[9] = ADC_Values[2] >> 8;
    sendData[10] = ADC_Values[2] & 0xff;
    sendData[11] = ADC_Values[3] >> 8;
    sendData[12] = ADC_Values[3] & 0xff;
    sendData[13] = chksum(sendData, 13);
    usb_send(sendData, 14);
}

